package com.medical.manage.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medical.common.core.domain.Result;
import com.medical.common.security.utils.SecurityUtils;
import com.medical.manage.domain.Appraise;
import com.medical.manage.domain.Order;
import com.medical.manage.domain.vo.AppraiseVo;
import com.medical.manage.mapper.AppraiseMapper;
import com.medical.manage.service.AppraiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: medical
 * @BelongsPackage: com.medical.manage.service.impl
 * @Author: szw
 * @CreateTime: 2025-03-18  16:15
 * @Description: TODO
 * @Version: 1.0
 */

@Service
public class AppraiseServiceImpl extends ServiceImpl<AppraiseMapper, Appraise> implements AppraiseService {
    @Autowired
    private AppraiseMapper appraiseMapper;

    /**
     * 评价医生
     * @param appraiseVo
     * @return
     */
    @Override
    public Result appraiseDoctor(AppraiseVo appraiseVo) {
        //非空判断
        if(appraiseVo.getAppraiseContent()==null || appraiseVo.getAppraiseContent()==""){
            return Result.error("评论内容不能为空");
        }
        Long userId = SecurityUtils.getUserId();
        appraiseVo.setPatientId(userId);
        //查询订单信息
        Order order=appraiseMapper.selectOrder(appraiseVo.getPatientId(),appraiseVo.getDoctorId());
        //判断订单状态，只有为1的时候才可以评价
        if(order.getState()==1){
            appraiseVo.setCreateTime(new Date());
            appraiseMapper.appraiseDoctor(appraiseVo);
        }
        return Result.success("评价成功");
    }

    /**
     * 患者评价
     * @return
     */
    @Override
    public Result<List<AppraiseVo>> getPatientAppraise() {
        Long userId = SecurityUtils.getUserId();
        List<AppraiseVo> list= appraiseMapper.getPatientAppraise(userId);
        return Result.success(list);
    }

    /**
     * 医生回复
     * @param appraiseVo
     * @return
     */
    @Override
    public Result doctorReply(AppraiseVo appraiseVo) {
        //非空判断
        if(appraiseVo.getReplyContent()==null || appraiseVo.getReplyContent().equals("")){
            return  Result.error("回复内容不能为空");
        }
        //患者id
        Long userId = SecurityUtils.getUserId();
        appraiseVo.setPatientId(userId);
        //添加评价记录
        appraiseVo.setReplyTime(new Date());
        appraiseMapper.addReplyContent(appraiseVo.getDoctorId(),appraiseVo.getPatientId(),appraiseVo.getReplyContent(),appraiseVo.getReplyTime());
        return Result.success("回复成功");
    }

}
